package com.example.new_music;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;

import com.example.new_music.adapter.welcomeAdapter;
import com.example.new_music.fragment.first;
import com.example.new_music.fragment.second;
import com.example.new_music.fragment.third;

import java.sql.Time;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    private ViewPager vp;
    private ArrayList<Fragment> list = new ArrayList<>();
    private RadioButton button1;
    private RadioButton button2;
    private RadioButton button3;
    private third third = new third();
    public static int time = 4;
    public static Timer timer;
    public static Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 11) {
                com.example.new_music.fragment.third.textView.setText(--time + "");
            } else if (msg.what == 12) {
                timer.cancel();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        SharedPreferences preferences = getSharedPreferences("first", MODE_PRIVATE);
        boolean first = preferences.getBoolean("first", false);
        if(first){
            Intent intent = new Intent(MainActivity.this, Main2Activity.class);
            startActivity(intent);
        }else{
            SharedPreferences.Editor edit = preferences.edit();
            edit.putBoolean("first",true);
            edit.commit();
        }
    }

    private void initView() {
        vp = (ViewPager) findViewById(R.id.vp);
        button1 = (RadioButton) findViewById(R.id.button1);
        button2 = (RadioButton) findViewById(R.id.button2);
        button3 = (RadioButton) findViewById(R.id.button3);
        first first = new first();
        second second = new second();
        list.add(first);
        list.add(second);
        list.add(third);
        welcomeAdapter welcomeAdapter = new welcomeAdapter(getSupportFragmentManager(), list);
        vp.setAdapter(welcomeAdapter);
        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                if (i == 0) {
                    button1.setChecked(true);
                } else if (i == 1) {
                    button2.setChecked(true);
                } else if (i == 2) {
                    time=4;
                    button3.setChecked(true);
                    timer = new Timer();
                    timer.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            if (time <= 0) {
                                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                                startActivity(intent);
                                timer.cancel();
                            }else {
                                Message message = new Message();
                                message.what = 11;
                                handler.sendMessage(message);
                            }
                        }
                    }, 0, 1000);
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }
}
